import React, {Component} from 'react';
import {
  View,
  TouchableOpacity,
  Image,
  Text,
  Animated,
} from 'react-native';

import ToastUtils from '../../common/ToastUtils';
import SizeUtils from '../../common/SizeUtils';
import SharePointAction from '../../actions/common/SharePointAction';


import BaseComponent from './BaseComponent';
import Constants from '../../common/Constants';
import Config from '../../config';
var WeChat=require('react-native-wechat');
const ICONS = [{url:require("../../../resource/images/common/wechat.png"), width:53, height:44, label:"微信好友"},
                {url:require("../../../resource/images/common/quan.png"), width:54, height:54, label:"朋友圈"}];

export default class CommonShareView extends Component {
  constructor(props) {
    super(props);


    this._hideY = SizeUtils.translateDesignHeightToCode(274);
    this.state = {
      showShare:false,
      valueXY: new Animated.ValueXY({x:0, y:this._hideY}),
    }
    WeChat.registerApp('wx3bc056ad73f10b81');
  }

  render() {
    return(
      <View style={{width:SizeUtils.getWindowSize().width, backgroundColor:"#00000066",
          height:SizeUtils.getWindowSize().height, flexDirection:"column"}}>
        <TouchableOpacity style={{width:SizeUtils.getWindowSize().width,
          height:SizeUtils.getWindowSize().height - SizeUtils.translateDesignHeightToCode(274),
          backgroundColor:"#00000000"}} onPress={this._onPressBg.bind(this)}>
        </TouchableOpacity>
        <Animated.View style={{width:SizeUtils.getWindowSize().width, height:SizeUtils.translateDesignHeightToCode(274),
            top:this.state.valueXY.y,
            backgroundColor:"#ffffff", opacity:1,
            paddingLeft:SizeUtils.translateDesignWidthToCode(98), paddingRight:SizeUtils.translateDesignWidthToCode(98),
            flex:1, flexDirection:"row", flexWrap:"wrap", justifyContent:"space-around"}}>
          {this._renderIcon(0)}
          {this._renderIcon(1)}

        </Animated.View>
      </View>
    )
  }

  componentDidMount() {
    this._showAnimation();
  }

  _onPressBg() {
    if(this.props.onClose) {
      this._closeAnimation(this.props.onClose);
    }
  }

  _renderIcon(index) {
    return (
      <TouchableOpacity onPress={() => {this._onPressIcon(index)}}
          style={{width:SizeUtils.translateDesignWidthToCode(100), paddingTop:SizeUtils.translateDesignHeightToCode(80)}}>
        <View style={{width:SizeUtils.translateDesignWidthToCode(100), height:SizeUtils.translateDesignHeightToCode(54),
            justifyContent:"center", alignItems:"center"}}>
          <Image source={ICONS[index].url} style={{width:SizeUtils.translateDesignWidthToCode(ICONS[index].width),
            height:SizeUtils.translateDesignHeightToCode(ICONS[index].height)}} />
        </View>
        <Text style={{paddingTop:SizeUtils.translateDesignHeightToCode(30), color:"#333333",
            fontSize:SizeUtils.translateFontSizeToCode(24), textAlign:"center"}}>
          {ICONS[index].label}
        </Text>
      </TouchableOpacity>
    )
  }

  _showAnimation() {
    this._clearAnimation();
    Animated.timing(
      this.state.valueXY,
        {toValue: {x:0, y:0},
        duration: 300}
    ).start();
  }

  _closeAnimation(callback) {
    this._clearAnimation();
    Animated.timing(
      this.state.valueXY,
        {toValue: {x:0, y:this._hideY},
        duration: 300}
    ).start(callback);
  }

  _clearAnimation() {
    this.state.valueXY.stopAnimation(
      ({value}) => {}
    )
  }

  _onPressIcon(index) {
    if(Constants.MODULE_TYPE.WORK_HARD == this.props.shareData.type){
      this.sharePost(index,Config.shareUrl + "postShare.html?post_id=" + this.props.shareData.post_id + "&board_id=" + this.props.shareData.board_id
      ,this.props.shareData.post_id,this.props.shareData.board_id == 1? 1:4);
    }
    else if(Constants.MODULE_TYPE.ACTIVITY == this.props.shareData.type){
      this.sharePost(index,Config.shareUrl+"activityShare.html?activity_id=" + this.props.shareData.activity_id + "&applicant_id=" + this.props.shareData.applicant_id
      ,this.props.shareData.applicant_id,3);
    }
    else if(Constants.MODULE_TYPE.NEWS == this.props.shareData.type){
      this.sharePost(index,Config.shareUrl+"newShare.html?new_id=" + this.props.shareData.news_id
      ,this.props.shareData.news_id,2);
    }
  }

  sharePost(index,url,id,type){
     new SharePointAction().call(this,{type:type,id:id});
     if(index == 0){
        WeChat.isWXAppInstalled()
                .then((isInstalled) => {
                  if (isInstalled) {

                    WeChat.shareToSession({
                      title:this.props.shareData.title,
                      description: this.props.shareData.title,
                      thumbImage: Config.shareUrl + "icon.png",
                      type: 'news',
                      webpageUrl: url
                    })
                    .catch((error) => {
                      console.log(error);
                      ToastUtils.show(error.message);
                    });
                    new SharePointAction().call(this,{type:type,id:id});
                    this._onPressBg();
                  } else {
                    ToastUtils.show('没有安装微信软件，请您安装微信之后再试');
                  }
                });
    }else if(index == 1){
      WeChat.isWXAppInstalled()
                .then((isInstalled) => {
                  if (isInstalled) {
                    WeChat.shareToTimeline({
                      title:this.props.shareData.title,
                      description: this.props.shareData.title,
                      thumbImage: Config.shareUrl + "icon.png",
                      type: 'news',
                      webpageUrl: url
                    })
                    .catch((error) => {
                      console.log(error);
                      ToastUtils.show(error.message);
                    });
                    new SharePointAction().call(this,{type:type,id:id});
                    this._onPressBg();
                  } else {
                    ToastUtils.show('没有安装微信软件，请您安装微信之后再试');
                  }
                });
    }
  }
}
